<?ob_start();
	@session_start();?>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>index</title>
	
 
	
</head>
<body>
	<?
		require_once('connect.php');
		
		//$cus_id=$_REQUEST['cus_id'];
		$cus_name=$_REQUEST['cus_name'];
		$cus_lname=$_REQUEST['cus_lname'];
		$cus_tel=$_REQUEST['cus_phone'];
		$cus_add=$_REQUEST['cus_address'];
		$user_name=$_REQUEST['user_name'];
		if($cus_name !="" && $cus_lname!="" && $cus_tel!="" && $cus_add!="" && $user_name!=""){
		//==================== Gen New ID  ==============
		$sql = mysql_query("select max(cus_id) as cus_id from customer ");
		if(mysql_num_rows($sql) !=0){
			$row = mysql_fetch_array($sql);
			$num = substr($row["cus_id"],1,4) + 1;
			if($num <=9){
				 $new_num = "c000".$num;
			}else if($num >9 && $num <=99 ){
				 $new_num = "c00".$num;
			}else if($num >99 && $num <=999 ){
				 $new_num = "c0".$num;
			}else if($num >999 && $num <=9999 ){
				 $new_num = "c".$num;
			}
		}
 		
	echo 	$strSQL=" INSERT INTO customer
				   VALUES ('".$new_num."','".$cus_name."','".$cus_lname."','".$cus_tel."','".$cus_add."','".$user_name."') ";
	
		$result=mysql_query($strSQL);
	
		if(!$result)
		{ 
			echo "<script> alert('เพิ่มข้อมมูลไม่สำเร็จ');  window.location.assign('add_cus.php')  </script>";
		}
		
		else {
			echo " <script> alert('สำเร็จ');  window.location.assign('add_cus.php')  </script>";
		}
	}else{
		echo "<script> alert('กรอกข้อมูลไม่ครบ');  window.history.back();  </script>";
	}
	?>


	

</body>
</html>